1418C - Mortal Kombat Tower - CodeForces Solution


dp graphs greedy shortest paths *1500

Please click on ads to support us..

Python Code:

from collections import deque
from collections import OrderedDict
import queue
import heapq
import math
import sys

sys.setrecursionlimit(10**6)
 
def sf(): return [int(x) for x in input().split(" ")]
def sfi(): return int(input())
def sfs(): return input()
def printf(x):
    print(x)
    sys.stdout.flush()



def main():
    t = int(input())
    for tc in range(t):
        n = int(input())
        dp = [0]*(n+5)
        bosses = [int(x) for x in input().split(" ")]
        bosses.append(0)

        for i in range(n-1,-1,-1):
                        cost1 = bosses[i] + min(dp[i+2],dp[i+3]) 

                        cost2 = bosses[i] + bosses[i+1] + min(dp[i+3],dp[i+4]) 

                        dp[i] = min(cost1, cost2)

        print(dp[0])


if __name__ == "__main__":
    main()

C++ Code:

#include <bits/stdc++.h>
#define MOD 1000000007;
#define MOD2 998244353;
#define IM ll_MAX;
#define IN ll_MIN;
#define mp make_pair;
#define ff first;
#define ss second;
#define PI 3.14159265;
#define N1 1e6;
#define N2 1e14;
using namespace std;
typedef long long ll;
typedef long long int lli;
typedef long double lld;
typedef unsigned int ui;
 
void Om_Nav() {
    ll npr;
    cin >> npr;
    vector<ll> nums(npr);
    for(ll idx = 0;idx < npr;idx++) {
        cin >> nums[idx];
    }
    vector<vector<ll>> DP;
    DP.resize(npr, vector<ll>(2));
    /*Base case*/
    DP[0][1] = nums[0];
    DP[0][0] = 2e9;
    /*begin value boss*/
    if(npr > 1) {
        DP[1][0] = DP[0][1];
        DP[1][1] = DP[0][1] + nums[1];
    }
    for(ll idx = 2;idx < npr;idx++) {
        /*prev boss and prev to prev boss*/
        DP[idx][0] = min(DP[idx-1][1], DP[idx-2][1]);
        /*min of curr bosses and prev boss sequence*/
        DP[idx][1] = min(DP[idx-1][0] + nums[idx], DP[idx-2][0] + nums[idx] + nums[idx-1]);
    }
    cout << min(DP[npr-1][0], DP[npr-1][1]) << "\n";
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll tpr;
    cin >> tpr;
    while(tpr--) {
        Om_Nav();
    }
	return 0;
}


Comments

Submit
0 Comments
More Questions

144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers